package com.yycx.module.bus.provider.service.impl;

import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaDelete;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.ApiAssert;
import com.yycx.module.bus.client.entity.BusSupplier;
import com.yycx.module.bus.provider.mapper.BusSupplierMapper;
import com.yycx.module.bus.provider.service.BusSupplierBankService;
import com.yycx.module.bus.provider.service.BusSupplierService;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.client.entity.SysOrganization;
import com.yycx.module.system.provider.service.SysCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;

/**
 * 供应商接口实现类
 *
 * @author flyme
 * @date 2020-07-24
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusSupplierServiceImpl extends BaseServiceImpl<BusSupplierMapper, BusSupplier> implements BusSupplierService {

    @Autowired
    private BusSupplierBankService supplierBankService;
    @Autowired
    private SysCompanyService companyService;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusSupplier supplier, EntityMap extra) {
        CriteriaQuery cq = new CriteriaQuery(BusSupplier.class);
        Long companyId = OpenHelper.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        Long organizationId = company.getOrganizationId();
        Long n = count(cq.eq(true, "supplierName", supplier.getSupplierName()).eq(true, "companyId", supplier.getCompanyId()).eq(true, "organizationId", organizationId));
        ApiAssert.lezero("供货商已存在", n.intValue());
        supplier.setSupplierState(true);
        supplier.setCompanyId(companyId);
        supplier.setOrganizationId(organizationId);
        return ResultBody.ok();
    }

    @Override
    public ResultBody beforeListEntityMap(CriteriaQuery<BusSupplier> cq, BusSupplier supplier, EntityMap requestMap) {
        cq.eq(SysCompany.class, "companyId", OpenHelper.getCompanyId());
        cq.eq(BusSupplier.class, "supplierState", CommonConstants.ENABLED);
        cq.createJoin(SysCompany.class);
        return super.beforeListEntityMap(cq, supplier, requestMap);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusSupplier> cq, BusSupplier supplier, EntityMap requestMap) {
        Long companyId = OpenHelper.getCompanyId();
        cq.select(BusSupplier.class, "*");
        cq.addSelect(SysCompany.class, "companyName");
        cq.addSelect(SysOrganization.class, "organizationName");
        cq.like(BusSupplier.class, "supplierName");
        cq.eq(BusSupplier.class, "companyId", companyId);
        cq.eq(BusSupplier.class, "organizationId");
        cq.orderByDesc("supplier.createTime");
        cq.createJoin(SysCompany.class);
        cq.createJoin(SysOrganization.class);
        return ResultBody.ok();
    }

    /**
     * 删除后业务处理
     *
     * @param cd
     * @param ids
     * @return
     */
    @Override
    public ResultBody afterDelete(CriteriaDelete cd, Serializable[] ids) {
        Serializable supplierId = cd.getIdValue();
        //删除银行账户
        supplierBankService.deleteBySupplierId(supplierId);
        return super.afterDelete(cd, ids);
    }
}
